package com.leadingsoft.pwxk.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.leadingsoft.pwxk.dto.CategoryDto;
import com.leadingsoft.pwxk.service.CategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author ysh
 * @desc 类别
 */
@Api(description = "2017国民经济行业类别")
@RestController
@RequestMapping("api/category")
@Slf4j
public class CategoryController {

	@Autowired
	CategoryService categoryService;

	@ApiOperation("导入")
	@PostMapping("upload")
	@ResponseStatus(code = HttpStatus.OK)
	public void upload(MultipartHttpServletRequest request) {
		try {
			categoryService.importData(request);
		} catch (IOException e) {
			log.error("导入数据异常" + e.getMessage(), e);
		}
	}

	@ApiOperation("获取所有类别树")
	@GetMapping
	@ResponseStatus(code = HttpStatus.OK)
	public List<CategoryDto> getAll() {
		return categoryService.getAll();
	}

	@ApiOperation("通过父类code获取子类")
	@GetMapping("/{code}")
	@ResponseStatus(code = HttpStatus.OK)
	public List<CategoryDto> getChilds(@PathVariable(value = "code") String code) {
		return categoryService.findByCodeChilds(code);
	}

	@ApiOperation("获取所有类别")
	@GetMapping("/list")
	@ResponseStatus(code = HttpStatus.OK)
	public List<CategoryDto> get() {
		return categoryService.get();
	}
}
